package com.wpt.main.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.wpt.main.constant.Constant;
import com.wpt.main.constant.FilesType;
import com.wpt.main.service.ISdCardService;
import com.wpt.main.util.FormatTimeUtil;
import com.wpt.main.vo.FileObject;

/**
 *	获取sd卡的文件操作
 *  @author   xinqh
 *  @Date	  2011-3-29  下午09:56:16
 */
public class SdCardService implements ISdCardService{
	
	private Comparator<FileObject> sortByModifyTime = new Comparator<FileObject>(){
		@Override
		public int compare(FileObject fo1, FileObject fo2) {
			return fo1.getModifyTime().compareTo(fo2.getModifyTime());
		}
	};

	/**
	 * 获取指定路径下的所有文件夹,用于左边树的显示
	 * @param path
	 * @return
	 */
	@Override
	public List<FileObject> getRootFileByPath() {
		List<FileObject> fos = new ArrayList<FileObject>();
		File file = new File(Constant.rootDisk);
		if(file.isDirectory()){
			File[] nextFiles = file.listFiles();//当前文件夹目录下的所有文件
			for(File nf : nextFiles){
				if(nf.isDirectory()){
					FileObject fo = new FileObject();
					setFileObjectValue(nf,fo,0);
					fos.add(fo);
				}
			}
		}
		Collections.sort(fos,sortByModifyTime);
		return fos;
	}
	
	/**
	 * 获取指定路径下的所有文件夹,用于左边树的显示
	 * @param path
	 * @return
	 */
	public List<FileObject> getLeftFileByPath(String path,int id){
		List<FileObject> fos = new ArrayList<FileObject>();
		File file = new File(path);
		if(file.isDirectory()){
			File[] nextFiles = file.listFiles();//当前文件夹目录下的所有文件
			for(File nf : nextFiles){
				if(nf.isDirectory()){
					FileObject fo = new FileObject();
					setFileObjectValue(nf,fo,id);
					fos.add(fo);
				}
			}
		}
		Collections.sort(fos,sortByModifyTime);
		return fos;
	}
	
	/**
	 * 获取指定路径下的所有文件夹和文件，用于右边内容的显示
	 * @param path
	 * @return
	 */
	@Override
	public List<FileObject> getFileByPath(String path,int id){
		List<FileObject> fos = new ArrayList<FileObject>();
		File file = new File(path);
		if(file.isDirectory()){
			File[] nextFiles = file.listFiles();//当前文件夹目录下的所有文件
			for(File nf : nextFiles){
				FileObject fo = new FileObject();
				setFileObjectValue(nf,fo,id);
				fos.add(fo);
			}
		}
		Collections.sort(fos,sortByModifyTime);
		return fos;
	}

	/**
	 * 增加文件夹
	 * @param id
	 * @param path
	 * @param fileName
	 * @return
	 */
	public List<FileObject> addFileByPath(int id,String path,String fileName){
		List<FileObject> fos = new ArrayList<FileObject>();
		File nf = new File(path+File.separator+fileName);
		if(nf.exists()){
			if(nf.isFile()){
				FileObject fo = new FileObject();
				setFileObjectValue(nf,fo,id);
				fos.add(fo);
			}
		}else{
			nf.mkdir();
			FileObject fo = new FileObject();
			setFileObjectValue(nf,fo,id);
			fos.add(fo);
		}
		return fos;
	}
	
	/**
	 * 删除文件
	 * @param path
	 * @param fileName
	 * @return
	 */
	public String deleteFileByPath(String path,String fileName){
		boolean flag = false;
		try {
			flag = del(new File(path));
		} catch (Exception e) {
			return "error";
		}
		return flag?"success":"error";
	}

	//删除目录及文件
	private boolean del(File f){
		boolean flag = false;
		if(f.isDirectory()){
			File[] subFile = f.listFiles();
			for(File sf : subFile){
				del(sf);
			}
		}
		flag = f.delete();
		return flag;
	}

	//根据文件来设置FileObject对象的值
	private void setFileObjectValue(File nf,FileObject fo,int id){
		String fileName = nf.getName();
		fo.setId(String.valueOf(id+1));
		fo.setName(fileName);
		fo.setPath(nf.getPath());
		if(nf.isDirectory()){
			fo.setLeaf("0");
			fo.setType("文件夹");
			fo.setLength("");
			fo.setPicture(FilesType.getFileIcon("folder"));
		}else{
			fo.setLeaf("1");
			fo.setLength(Math.round(nf.length()/1024+0.5)+"KB");
			int i = fileName.lastIndexOf(".");
			if(i!=-1){
				String suffix = fileName.substring(i);
				fo.setType(FilesType.getFileType(suffix));
				
				fo.setPicture(FilesType.getFileIcon(suffix));
			}else{
				fo.setType("其他");
				fo.setPicture(FilesType.getFileIcon("other"));
			}
		}
		fo.setModifyTime(FormatTimeUtil.formatTime(nf.lastModified()));
	}
	
}

